package com.atguigu.crowd.mvc;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.service.impl.AdminServiceImpl;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AdminService adminService;

    @ResponseBody
    @RequestMapping("test/ajax/async.html")
    public String testAsync() throws InterruptedException {
        Thread.sleep(2000);
        return "success";
    }


    //返回格式为json 请求后缀名为json才能解析
//    @ResponseBody
//    @RequestMapping(value = "send/compose/object.json")
//    public ResultEntity<Student> testReceiveComposeObject(@RequestBody Student student, HttpServletRequest request) {
//        boolean judgeRequestType = CrowdUtil.judgeRequestType(request);
//        logger.info("judgeRequestType=" + judgeRequestType);
//        logger.info(student.toString());
//        //将“查询”到的Student对象封装到ResultEntity中返回
//        String a = null;
//        System.out.println(a.length());
//        ResultEntity<Student> resultEntity = ResultEntity.successWithData(student);
//        return resultEntity;
//    }

    @ResponseBody
    @RequestMapping("send/array/three.html")
    public String testReceiveArrayThree(@RequestBody List<Integer> array) {
        for (Integer number : array) {
            logger.info("number=" + number);
        }
        return "success";
    }





    @RequestMapping("send/array/one.html")
    public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array) {
        for (Integer number : array) {
            System.out.println("number=" + number);
        }
        return "target";
    }

    @RequestMapping("/test/ssm.html")
    public String testSsm(ModelMap modelMap, HttpServletRequest request) {
        boolean judgeRequestType = CrowdUtil.judgeRequestType(request);
        logger.info("judgeRequestType=" + judgeRequestType);
        List<Admin> adminList = adminService.getAll();
//        String a = null;
//        System.out.println(a.length());
        System.out.println(10/0);
        modelMap.addAttribute("adminList", adminList);
        return "target";
    }
}
